package com.vancouver.knows.business;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import android.annotation.SuppressLint;

import com.vancouver.knows.business.bean.DiscountCategoryInfo;
import com.vancouver.knows.business.bean.DiscountInfo;
import com.vancouver.knows.business.bean.EventInfo;
import com.vancouver.knows.business.bean.EventItemInfo;
import com.vancouver.knows.business.bean.MyArticleInfo;
import com.vancouver.knows.business.bean.MyAtcommentsInfo;
import com.vancouver.knows.business.bean.MyCommentsInfo;
import com.vancouver.knows.business.bean.MyFollowedInfo;

/**
 * 工具类
 */
public class BUtils {

	public static final String sdfStr = "yyyy-MM-dd";
	public static final String sdfTimeStr = "yyyy-MM-dd'T'HH:mm:ss";
	public static final String sdfHmsStr = "yyyy-MM-dd HH:mm:ss";

	public static class EventInfoSorter implements Comparator<EventInfo> {

		@Override
		public int compare(EventInfo o1, EventInfo o2) {
			if (o1.getEventPositionRanking() > o2.getEventPositionRanking()) {
				return 1;

			} else if (o1.getEventPositionRanking() < o2.getEventPositionRanking()) {
				return -1;

			} else {
				return 0;
			}
		}
	}

	public static String getEventPdg(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(sdfTimeStr, Locale.CANADA);
		try {
			sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
			Date date = sdf.parse(dateStr);
			sdf.setTimeZone(TimeZone.getDefault());
			return sdf.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return dateStr;
		}
	}

	@SuppressLint("SimpleDateFormat")
	private static long getGMTTime(String gmtTime, String fromTime) {
		long timeLogn = 0;
		SimpleDateFormat sdf = new SimpleDateFormat(fromTime);
		try {
			Date date = sdf.parse(gmtTime);
			timeLogn = date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return timeLogn;
	}

	public static class myArticleInfoSorter implements Comparator<MyArticleInfo> {

		private long getPdgLong(String pdg) {
			return getGMTTime(pdg, sdfTimeStr);
		}

		@Override
		public int compare(MyArticleInfo o1, MyArticleInfo o2) {
			long long1 = getPdgLong(o1.getPostDateGmt());
			long long2 = getPdgLong(o2.getPostDateGmt());
			if (long1 > long2) {
				return -1;

			} else if (long1 < long2) {
				return 1;

			} else {
				return 0;
			}
		}
	}

	@SuppressLint("SimpleDateFormat")
	public static class MyFollowedSorter implements Comparator<MyFollowedInfo> {

		private long getDagLong(String dag) {
			return getGMTTime(dag, sdfHmsStr);
		}

		@Override
		public int compare(MyFollowedInfo o1, MyFollowedInfo o2) {
			long long1 = getDagLong(o1.getDateAddedGmt());
			long long2 = getDagLong(o2.getDateAddedGmt());
			if (long1 > long2) {
				return -1;

			} else if (long1 < long2) {
				return 1;

			} else {
				return 0;
			}
		}
	}

	public static class myCommentsInfoSorter implements Comparator<MyCommentsInfo> {

		private long getCdgLong(String cdg) {
			return getGMTTime(cdg, sdfTimeStr);
		}

		@Override
		public int compare(MyCommentsInfo o1, MyCommentsInfo o2) {
			long long1 = getCdgLong(o1.getcDateGmt());
			long long2 = getCdgLong(o2.getcDateGmt());
			if (long1 > long2) {
				return -1;

			} else if (long1 < long2) {
				return 1;

			} else {
				return 0;
			}
		}
	}

	public static class myAtcommentsInfoSorter implements Comparator<MyAtcommentsInfo> {

		private long getCdgLong(String cdg) {
			return getGMTTime(cdg, sdfTimeStr);
		}

		@Override
		public int compare(MyAtcommentsInfo o1, MyAtcommentsInfo o2) {
			long long1 = getCdgLong(o1.getcDateGmt());
			long long2 = getCdgLong(o2.getcDateGmt());
			if (long1 > long2) {
				return -1;

			} else if (long1 < long2) {
				return 1;

			} else {
				return 0;
			}
		}
	}

	public static class DiscountInfosSorter implements Comparator<DiscountInfo> {

		@Override
		public int compare(DiscountInfo o1, DiscountInfo o2) {
			double d1 = o1.getHomeRanking();
			double d2 = o2.getHomeRanking();
			if (d1 < d2) {
				return 1;

			} else if (d1 > d2) {
				return -1;

			} else {
				return 0;
			}
		}
	}

	public static class CategoryDiscountInfosSorter implements Comparator<DiscountInfo> {

		@Override
		public int compare(DiscountInfo o1, DiscountInfo o2) {
			String d1 = o1.getCategoryTop();
			String d2 = o2.getCategoryTop();
			if ("Y".equals(d1)) {
				return -1;

			} else if ("Y".equals(d2)) {
				return 1;

			} else {
				return 0;
			}
		}
	}

	public static class DiscountCategoryInfosSorter implements Comparator<DiscountCategoryInfo> {

		@Override
		public int compare(DiscountCategoryInfo o1, DiscountCategoryInfo o2) {
			double d1 = o1.getPosition();
			double d2 = o2.getPosition();
			if (d1 > d2) {
				return 1;

			} else if (d1 < d2) {
				return -1;

			} else {
				return 0;
			}
		}
	}

	public static boolean isLaterCurrentDate(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat(sdfStr);
		sdf.setTimeZone(TimeZone.getTimeZone("UTC-8"));
		Date date = null;
		try {
			date = sdf.parse(time);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (date == null) {
			return false;
		}

		Date currentDate = new Date();
		currentDate.setHours(0);
		currentDate.setMinutes(0);
		currentDate.setSeconds(0);

		if (date.getTime() >= currentDate.getTime()) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isCollectionEmpty(Collection collection){
		if(collection==null || collection.isEmpty()){
			return true;
		}

		return false;
	}
}
